package com.cmccgz.udacs_landlord_webapp.main.housemanager.list;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.baselibrary.utils.InputMethodUtil;
import com.cmccgz.baselibrary.view.ClearEditText;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.HouseListAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.HouseListDataBean;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/5 09:10
 */

public class HouseListFragment extends BaseFragment implements HouseListContract.HouseListView,
        View.OnClickListener, XRecyclerView.LoadingListener, ClearEditText.OnTextChangeListener {

    public static final String TYPE_ROOM = "0";
    public static final String TYPE_BUILDING = "1";
    private HouseListContract.HouseListPresenter mPresenter;
    private TextView tv_title;
    private int type;
    private int page = 1;
    private TextView tv_back;
    private ClearEditText et_search;
    private TextView btn_search;
    private XRecyclerView rv_list;
    private String keyword = "";
    private HouseListAdapter adapter;
    private boolean isForSelected;
    private InputMethodUtil inputMethodUtil;

    public HouseListFragment() {
    }

    public static HouseListFragment newInstance() {
        return new HouseListFragment();
    }

    @Override
    public void setPresenter(HouseListContract.HouseListPresenter presenter) {
        mPresenter = presenter;
    }


    @Override
    protected int setLayoutId() {
        return R.layout.fragment_houselist;
    }

    @Override
    protected void initView() {
        getBundle();
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_back.setVisibility(View.VISIBLE);
        et_search = (ClearEditText) findViewById(R.id.et_search);
        btn_search = (TextView) findViewById(R.id.btn_search);
        rv_list = (XRecyclerView) findViewById(R.id.rv_list);
        initTitleSearchType();
        initListener();
        initRecyclerView();
        initInputMethod();
        mPresenter.start();
    }

    private void initInputMethod() {
        et_search.setOnTextChangeListener(this);
        inputMethodUtil = new InputMethodUtil(mContext);
        inputMethodUtil.setupUI(root, et_search);
        inputMethodUtil.setSearchBtn(et_search,btn_search);
    }

    private void initRecyclerView() {
        adapter = new HouseListAdapter(mContext, type, isForSelected);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(layoutManager);
        rv_list.setAdapter(adapter);
        rv_list.setRefreshProgressStyle(ProgressStyle.SysProgress);
        rv_list.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        rv_list.setLoadingListener(this);
        rv_list.addItemDecoration(new MyDividerItemDecoration(mContext, DividerItemDecoration.VERTICAL, 4, getResources().getColor(R.color.transparent)));
    }

    private void initListener() {
        tv_back.setOnClickListener(this);
        btn_search.setOnClickListener(this);
    }

    private void initTitleSearchType() {
        switch (type) {
            case HouseListActivity.TYPE_CHECKOUT:
                tv_title.setText("退房");
                et_search.setHint("请输入房屋名称");
                mPresenter.setSearchType(TYPE_ROOM);
                break;
            case HouseListActivity.TYPE_EXCHANGE:
                tv_title.setText("换房");
                et_search.setHint("请输入房屋名称");
                mPresenter.setSearchType(TYPE_ROOM);
                break;
            case HouseListActivity.TYPE_ROOM_COUNT:
                tv_title.setText("出租统计");
                et_search.setHint("请输入楼栋名称");
                mPresenter.setSearchType(TYPE_BUILDING);
                break;
            case HouseListActivity.TYPE_SELECT_BUILDING:
                tv_title.setText("选择楼栋");
                et_search.setHint("请输入楼栋名称");
                mPresenter.setSearchType(TYPE_BUILDING);
                break;
            case HouseListActivity.TYPE_SELECT_HOUSE:
                tv_title.setText("选择房屋");
                et_search.setHint("请输入房屋名称");
                mPresenter.setSearchType(TYPE_ROOM);
                break;
            default:
                break;
        }
    }

    private void getBundle() {
        Bundle bundle = getArguments();
        type = bundle.getInt(HouseListActivity.TYPE, HouseListActivity.TYPE_NULL);
        isForSelected = bundle.getBoolean(HouseListActivity.IS_FOR_SELECTED, false);
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void showLoading() {
        rv_list.setRefreshing(true);
    }

    @Override
    public void refreshDataSuccess(List<HouseListDataBean> houseList) {
        rv_list.refreshComplete();
        adapter.notify(houseList);
    }

    @Override
    public void refreshDataFailed(String errorMsg) {
        rv_list.refreshComplete();
        showToast(errorMsg);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadMoreDataSuccess(List<HouseListDataBean> houseList) {
        rv_list.loadMoreComplete();
        if (houseList.size() == 0) {
            page--;
        } else {
            adapter.notifyMore(houseList);
        }
    }

    @Override
    public void loadMoreDataFailed(String errorMsg) {
        rv_list.loadMoreComplete();
        page--;
        showToast(errorMsg);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                getActivity().finish();
                break;
            case R.id.btn_search:
                hideSoftKeyboard(v);
                keyword = et_search.getText().toString().trim();
                showLoading();
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.getData(keyword, "", page);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.getData(keyword, "", page);
    }

    @Override
    public void onTextChange(String text) {

    }

    @Override
    public void onClearText() {
        keyword = "";
        showLoading();
        Logger.i("搜索框清空重新加载");
    }

}
